﻿using System;

namespace ExFlash.Records
{
    public class SoundInfo : ISwfSerializable
    {

        public Boolean SyncStop
        {
            get;
            private set;
        }

        public Boolean SyncNoMultiple
        {
            get;
            private set;
        }

        public Boolean HasEnvelope
        {
            get;
            private set;
        }
        
        public Boolean HasLoops
        {
            get;
            private set;
        }

        public Boolean HasOutPoint
        {
            get;
            private set;
        }

        public Boolean HasInPoint
        {
            get;
            private set;
        }

        public UInt32 InPoint
        {
            get;
            private set;
        }

        public UInt32 OutPoint
        {
            get;
            private set;
        }

        public UInt16 LoopCount
        {
            get;
            private set;
        }

        public Byte EnvPoints
        {
            get;
            private set;
        }

        public SoundEnvelope[] EnvelopeRecords
        {
            get;
            private set;
        }

        #region ISwfSerializable Members

        public void SwfDeserialize(BinaryBitReader reader)
        {
            // Reserved
            reader.SkipBits(2);

            SyncStop = reader.ReadBoolean();
            SyncNoMultiple = reader.ReadBoolean();
            HasEnvelope = reader.ReadBoolean();
            HasLoops = reader.ReadBoolean();
            HasOutPoint = reader.ReadBoolean();
            HasInPoint = reader.ReadBoolean();
            reader.AlignBits();

            if (HasInPoint)
            {
                InPoint = reader.ReadUInt32();
            }

            if (HasOutPoint)
            {
                OutPoint = reader.ReadUInt32();
            }

            if (HasLoops)
            {
                LoopCount = reader.ReadUInt16();
            }

            if (HasEnvelope)
            {
                EnvPoints = reader.ReadByte();
                EnvelopeRecords = new SoundEnvelope[EnvPoints];

                for (int i = 0; i < EnvPoints; i++)
                {
                    EnvelopeRecords[i] = new SoundEnvelope();
                    EnvelopeRecords[i].SwfDeserialize(reader);
                }
            }
        }

        public void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
